package cn.bocweb.spinning.view.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.StartExamBean;
import cn.bocweb.spinning.view.activity.TestActivity;

/**
 * Created by chenchangxing on 15/12/23.
 */

@SuppressLint("ValidFragment")
public class ExamQuizFragment extends Fragment implements View.OnClickListener {


    int resId, i;
    @Bind(R.id.exam_quiz_type)
    TextView examQuizType;
    @Bind(R.id.exam_quiz_quiz)
    TextView examQuizQuiz;
    @Bind(R.id.exam_quiz_judge_a_icon)
    ImageView examQuizJudgeAIcon;
    @Bind(R.id.exam_quiz_judge_a)
    LinearLayout examQuizJudgeA;
    @Bind(R.id.exam_quiz_judge_b_icon)
    ImageView examQuizJudgeBIcon;
    @Bind(R.id.exam_quiz_judge_b)
    LinearLayout examQuizJudgeB;
    @Bind(R.id.exam_quiz_judge)
    LinearLayout examQuizJudge;
    @Bind(R.id.exam_quiz_select_a_icon)
    ImageView examQuizSelectAIcon;
    @Bind(R.id.exam_quiz_select_a)
    LinearLayout examQuizSelectA;
    @Bind(R.id.exam_quiz_select_b_icon)
    ImageView examQuizSelectBIcon;
    @Bind(R.id.exam_quiz_select_b)
    LinearLayout examQuizSelectB;
    @Bind(R.id.exam_quiz_select_c_icon)
    ImageView examQuizSelectCIcon;
    @Bind(R.id.exam_quiz_select_c)
    LinearLayout examQuizSelectC;
    @Bind(R.id.exam_quiz_select_d_icon)
    ImageView examQuizSelectDIcon;
    @Bind(R.id.exam_quiz_select_d)
    LinearLayout examQuizSelectD;
    @Bind(R.id.exam_quiz_select_e_icon)
    ImageView examQuizSelectEIcon;
    @Bind(R.id.exam_quiz_select_e)
    LinearLayout examQuizSelectE;
    @Bind(R.id.exam_quiz_select_f_icon)
    ImageView examQuizSelectFIcon;
    @Bind(R.id.exam_quiz_select_f)
    LinearLayout examQuizSelectF;
    @Bind(R.id.exam_quiz_select)
    LinearLayout examQuizSelect;

    StartExamBean.DataEntity dataEntities;
    TestActivity testActivity;
    Timer timer;
    Map<Integer, Boolean> answerList;
    int position, pos;
    int a[] = {0, 0, 0, 0, 0, 0};
    Boolean changge = false;

    String selectAnswer = "";
    String selected = "";
    String isChange = "0";
    StringBuffer results = new StringBuffer();
    boolean isSecondTest;
    int type, quizNum = 0;
    @Bind(R.id.test_select_a)
    TextView testSelectA;
    @Bind(R.id.test_select_b)
    TextView testSelectB;
    @Bind(R.id.test_select_c)
    TextView testSelectC;
    @Bind(R.id.test_select_d)
    TextView testSelectD;
    @Bind(R.id.test_select_e)
    TextView testSelectE;
    @Bind(R.id.test_select_f)
    TextView testSelectF;
    @Bind(R.id.test_detail)
    TextView testDetail;
    @Bind(R.id.test_explain_layout)
    LinearLayout testExplainLayout;
    @Bind(R.id.test_layout)
    LinearLayout testLayout;


    @SuppressLint("ValidFragment")
    public ExamQuizFragment(StartExamBean.DataEntity dataEntities, int position, boolean isSecondTest, String isChange) {
        this.dataEntities = dataEntities;
        this.position = position;
        this.isSecondTest = isSecondTest;
        this.isChange = isChange;
    }

    public void setLayoutVisible(boolean isVisible) {
        testLayout.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exam_quiz, container, false);
        ButterKnife.bind(this, view);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        testActivity = (TestActivity) getActivity();
        timer = new Timer();
        answerList = new HashMap<>();

        initTitle();
        initClickBtn();
        initExplain();
    }

    @Override
    public void onStart() {
        super.onStart();

//        testActivity = (TestActivity) getActivity();
//        timer = new Timer();
//        answerList = new HashMap<>();
//
//        initTitle();
//        initClickBtn();
//        initExplain();
    }


    private void initExplain() {
        testDetail.setText(dataEntities.getExplain());
    }


    private void initTitle() {
        type = Integer.parseInt(dataEntities.getTtype());
        if (type != 2) {
            for (int i1 = 0; i1 < dataEntities.getOption().size(); i1++) {
                if (dataEntities.getOption() != null && !dataEntities.getOption().get(i1).equals("")) {
                    quizNum++;
                }
            }
        }

        switch (type) {
            case 1: {
                examQuizQuiz.setText("\t\t\t\t\t\t        " + dataEntities.getTitle());
                examQuizSelect.setVisibility(View.VISIBLE);
                examQuizJudge.setVisibility(View.GONE);
                examQuizType.setText("【单选】");
                initSelectQuiz();
                if (dataEntities.getRight().size() == 1) {
                    selectAnswer = dataEntities.getRight().get(0);
                }
                testDetail.setText(dataEntities.getExplain());
                break;
            }
            case 2: {

                examQuizQuiz.setText("\t\t\t\t\t\t        " + dataEntities.getTitle());
                examQuizSelect.setVisibility(View.GONE);
                examQuizJudge.setVisibility(View.VISIBLE);
                examQuizType.setText("【判断】");
                if (dataEntities.getRight().size() == 1) {
                    selectAnswer = dataEntities.getRight().get(0);
                }
                Log.e("=====", "right" + dataEntities.getRight());
                testDetail.setText(dataEntities.getExplain());
                break;
            }
            //新加入不定项选择
            case 3: {

                examQuizQuiz.setText("\t\t\t\t\t\t\t         " + dataEntities.getTitle());
                examQuizSelect.setVisibility(View.VISIBLE);
                examQuizJudge.setVisibility(View.GONE);
                examQuizType.setText("【不定项】");
                examQuizType.setTextColor(getResources().getColor(R.color.red1));
                StringBuffer selectAnswers = new StringBuffer();

                for (int i1 = 0; i1 < dataEntities.getRight().size(); i1++) {
                    selectAnswers.append(Integer.parseInt(dataEntities.getRight().get(i1)));
                }
                selectAnswer = selectAnswers.toString();

                testDetail.setText(dataEntities.getExplain());
                initSelectQuiz();
                break;
            }
        }
    }

    private void initSelectQuiz() {
        examQuizSelectA.setVisibility(View.GONE);
        examQuizSelectB.setVisibility(View.GONE);
        examQuizSelectC.setVisibility(View.GONE);
        examQuizSelectD.setVisibility(View.GONE);
        examQuizSelectE.setVisibility(View.GONE);
        examQuizSelectF.setVisibility(View.GONE);
        switch (quizNum) {
            case 1:
                testSelectA.setText(dataEntities.getOption().get(0));
                examQuizSelectA.setVisibility(View.VISIBLE);
                break;
            case 2:
                testSelectA.setText(dataEntities.getOption().get(0));
                testSelectB.setText(dataEntities.getOption().get(1));
                examQuizSelectA.setVisibility(View.VISIBLE);
                examQuizSelectB.setVisibility(View.VISIBLE);
                break;
            case 3:
                testSelectA.setText(dataEntities.getOption().get(0));
                testSelectB.setText(dataEntities.getOption().get(1));
                testSelectC.setText(dataEntities.getOption().get(2));
                examQuizSelectA.setVisibility(View.VISIBLE);
                examQuizSelectB.setVisibility(View.VISIBLE);
                examQuizSelectC.setVisibility(View.VISIBLE);
                break;
            case 4:
                testSelectA.setText(dataEntities.getOption().get(0));
                testSelectB.setText(dataEntities.getOption().get(1));
                testSelectC.setText(dataEntities.getOption().get(2));
                testSelectD.setText(dataEntities.getOption().get(3));
                examQuizSelectA.setVisibility(View.VISIBLE);
                examQuizSelectB.setVisibility(View.VISIBLE);
                examQuizSelectC.setVisibility(View.VISIBLE);
                examQuizSelectD.setVisibility(View.VISIBLE);
                break;
            case 5:
                testSelectA.setText(dataEntities.getOption().get(0));
                testSelectB.setText(dataEntities.getOption().get(1));
                testSelectC.setText(dataEntities.getOption().get(2));
                testSelectD.setText(dataEntities.getOption().get(3));
                testSelectE.setText(dataEntities.getOption().get(4));
                examQuizSelectA.setVisibility(View.VISIBLE);
                examQuizSelectB.setVisibility(View.VISIBLE);
                examQuizSelectC.setVisibility(View.VISIBLE);
                examQuizSelectD.setVisibility(View.VISIBLE);
                examQuizSelectE.setVisibility(View.VISIBLE);
                break;
            case 6:
                testSelectA.setText(dataEntities.getOption().get(0));
                testSelectB.setText(dataEntities.getOption().get(1));
                testSelectC.setText(dataEntities.getOption().get(2));
                testSelectD.setText(dataEntities.getOption().get(3));
                testSelectE.setText(dataEntities.getOption().get(4));
                testSelectF.setText(dataEntities.getOption().get(5));
                examQuizSelectA.setVisibility(View.VISIBLE);
                examQuizSelectB.setVisibility(View.VISIBLE);
                examQuizSelectC.setVisibility(View.VISIBLE);
                examQuizSelectD.setVisibility(View.VISIBLE);
                examQuizSelectE.setVisibility(View.VISIBLE);
                examQuizSelectF.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    private void initClickBtn() {
        examQuizJudgeA.setOnClickListener(this);
        examQuizJudgeB.setOnClickListener(this);

        examQuizSelectA.setOnClickListener(this);
        examQuizSelectB.setOnClickListener(this);
        examQuizSelectC.setOnClickListener(this);
        examQuizSelectD.setOnClickListener(this);
        examQuizSelectE.setOnClickListener(this);
        examQuizSelectF.setOnClickListener(this);

    }


    public boolean initSelectResult() {


        if (testActivity == null) {
            testActivity = (TestActivity) getActivity();
        }
        if (type == 3) {
            StringBuffer sbf = new StringBuffer();
            int[] num = new int[results.length()];
            for (int i1 = 0; i1 < results.length(); i1++) {
                num[i1] = results.charAt(i1);
            }
            Arrays.sort(num);
            for (int i1 = 0; i1 < num.length; i1++) {
                sbf.append(num[i1]);
            }
            selected = sbf.toString();

        }
        if (!selected.equals("") || selected.equals("0")) {
            testActivity.addSelectAnswer(position, selected);
            if (selected.equals(selectAnswer)) {

                if (testActivity == null) {
                    testActivity = (TestActivity) getActivity();
                }
                testActivity.addQuizCorrect(position, true, type);
                checkIsAllAnswer();
            } else {
//            testExplainLayout.setVisibility(View.VISIBLE);
                if (testActivity == null) {
                    testActivity = (TestActivity) getActivity();
                }
                testActivity.addQuizCorrect(position, false, type);
                checkIsAllAnswer();
            }

            return true;
        } else {
            return false;
        }


    }

    private void subSelectResult(String result) {
        clearSelectResult();
        switch (result) {
            case "1": {
                examQuizSelectAIcon.setImageResource(R.mipmap.gou);
                selected = "1";
                break;
            }
            case "2": {
                examQuizSelectBIcon.setImageResource(R.mipmap.gou);
                selected = "2";
                break;
            }
            case "3": {
                examQuizSelectCIcon.setImageResource(R.mipmap.gou);
                selected = "3";
                break;
            }
            case "4": {
                examQuizSelectDIcon.setImageResource(R.mipmap.gou);
                selected = "4";
                break;
            }
            case "5": {
                examQuizSelectEIcon.setImageResource(R.mipmap.gou);
                selected = "5";
                break;
            }
            case "6": {
                examQuizSelectFIcon.setImageResource(R.mipmap.gou);
                selected = "6";
                break;
            }
        }

    }

    private void clearSelectResult() {
        selected = "0";
        examQuizSelectAIcon.setImageResource(R.mipmap.exam_a);

        examQuizSelectBIcon.setImageResource(R.mipmap.exam_b);

        examQuizSelectCIcon.setImageResource(R.mipmap.exam_c);

        examQuizSelectDIcon.setImageResource(R.mipmap.exam_d);

        examQuizSelectEIcon.setImageResource(R.mipmap.exam_e);

        examQuizSelectFIcon.setImageResource(R.mipmap.exam_f);
    }

    private void addSelectResult(String result) {

        results.append(result);
        switch (result) {
            case "1": {
                examQuizSelectAIcon.setImageResource(R.mipmap.gou);

                break;
            }
            case "2": {
                examQuizSelectBIcon.setImageResource(R.mipmap.gou);

                break;
            }
            case "3": {
                examQuizSelectCIcon.setImageResource(R.mipmap.gou);

                break;
            }
            case "4": {
                examQuizSelectDIcon.setImageResource(R.mipmap.gou);

                break;
            }
            case "5": {
                examQuizSelectEIcon.setImageResource(R.mipmap.gou);

                break;
            }
            case "6": {
                examQuizSelectFIcon.setImageResource(R.mipmap.gou);

                break;
            }
        }

    }

    private void deleteSelectResult(String result) {
        int a = results.indexOf(result);
        results.delete(a, a + 1);
        switch (result) {
            case "1": {
                examQuizSelectAIcon.setImageResource(R.mipmap.exam_a);

                break;
            }
            case "2": {
                examQuizSelectBIcon.setImageResource(R.mipmap.exam_b);

                break;
            }
            case "3": {
                examQuizSelectCIcon.setImageResource(R.mipmap.exam_c);

                break;
            }
            case "4": {
                examQuizSelectDIcon.setImageResource(R.mipmap.exam_d);

                break;
            }
            case "5": {
                examQuizSelectEIcon.setImageResource(R.mipmap.exam_e);

                break;
            }
            case "6": {
                examQuizSelectFIcon.setImageResource(R.mipmap.exam_f);

                break;
            }
        }

    }


    private void drawSelectResult(ImageView select) {
        examQuizSelectAIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizSelectBIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizSelectCIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizSelectDIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizSelectEIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizSelectFIcon.setImageResource(R.mipmap.exam_answer_wrong);

        select.setImageResource(R.mipmap.gou);
    }

    private void initJudgeResult(String result) {
        if (testActivity == null) {
            testActivity = (TestActivity) getActivity();
        }
        selected = String.valueOf(result);

//        TimerTask timerTask = new TimerTask() {
//            @Override
//            public void run() {
//                testActivity.selectRightAnswer(position + 1);
//            }
//        };
//
//        timer = new Timer();
//        timer.schedule(timerTask, 1000);


        switch (Integer.valueOf(result)) {
            case 1: {
                examQuizJudgeBIcon.setImageResource(R.mipmap.exam_b);
                examQuizJudgeAIcon.setImageResource(R.mipmap.gou);

                break;
            }
            case 2: {
                examQuizJudgeAIcon.setImageResource(R.mipmap.exam_a);
                examQuizJudgeBIcon.setImageResource(R.mipmap.gou);
                break;
            }
        }
    }

    private void drawJudgeRuselt(ImageView judge) {
        examQuizJudgeAIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizJudgeBIcon.setImageResource(R.mipmap.exam_answer_wrong);

        judge.setImageResource(R.mipmap.gou);
    }

    public void setAllClickable(String isChanged) {
        if (isChanged.equals("0")) {
            examQuizJudgeA.setClickable(false);
            examQuizJudgeB.setClickable(false);

            examQuizSelectA.setClickable(false);
            examQuizSelectB.setClickable(false);
            examQuizSelectC.setClickable(false);
            examQuizSelectD.setClickable(false);
            examQuizSelectE.setClickable(false);
            examQuizSelectF.setClickable(false);
        } else {
            examQuizJudgeA.setClickable(true);
            examQuizJudgeB.setClickable(true);

            examQuizSelectA.setClickable(true);
            examQuizSelectB.setClickable(true);
            examQuizSelectC.setClickable(true);
            examQuizSelectD.setClickable(true);
            examQuizSelectE.setClickable(true);
            examQuizSelectF.setClickable(true);
        }

    }

    @Override
    public void onClick(View v) {

        if (type != 0 && type != 3) {
            switch (v.getId()) {
                case R.id.exam_quiz_judge_a: {
                    initJudgeResult("1");
                    checkIsAllAnswer();

                    break;
                }
                case R.id.exam_quiz_judge_b: {
                    initJudgeResult("2");
                    checkIsAllAnswer();
                    break;
                }
                case R.id.exam_quiz_select_a: {
                    subSelectResult("1");
                    checkIsAllAnswer();
                    break;
                }
                case R.id.exam_quiz_select_b: {
                    subSelectResult("2");
                    checkIsAllAnswer();
                    break;
                }
                case R.id.exam_quiz_select_c: {
                    subSelectResult("3");
                    checkIsAllAnswer();
                    break;
                }
                case R.id.exam_quiz_select_d: {
                    subSelectResult("4");
                    checkIsAllAnswer();
                    break;
                }
                case R.id.exam_quiz_select_e: {
                    subSelectResult("5");
                    checkIsAllAnswer();
                    break;
                }
                case R.id.exam_quiz_select_f: {
                    subSelectResult("6");
                    checkIsAllAnswer();
                    break;
                }
            }
        } else {
            switch (v.getId()) {
                case R.id.exam_quiz_select_a: {
                    a[0]++;
                    if (a[0] % 2 == 1) {
                        addSelectResult("1");
                    } else {
                        deleteSelectResult("1");
                    }
                    break;
                }
                case R.id.exam_quiz_select_b: {
                    a[1]++;
                    if (a[1] % 2 == 1) {
                        addSelectResult("2");
                    } else {
                        deleteSelectResult("2");
                    }
                    break;
                }
                case R.id.exam_quiz_select_c: {
                    a[2]++;
                    if (a[2] % 2 == 1) {
                        addSelectResult("3");
                    } else {
                        deleteSelectResult("3");
                    }
                    break;
                }
                case R.id.exam_quiz_select_d: {
                    a[3]++;
                    if (a[3] % 2 == 1) {
                        addSelectResult("4");
                    } else {
                        deleteSelectResult("4");
                    }
                    break;
                }
                case R.id.exam_quiz_select_e: {
                    a[4]++;
                    if (a[4] % 2 == 1) {
                        addSelectResult("5");
                    } else {
                        deleteSelectResult("5");
                    }
                    break;
                }
                case R.id.exam_quiz_select_f: {
                    a[5]++;
                    if (a[5] % 2 == 1) {
                        addSelectResult("6");
                    } else {
                        deleteSelectResult("6");
                    }
                    break;
                }
            }
        }
    }

    private void checkIsAllAnswer() {
        if (testActivity.isAllAnswer()) {
            testActivity.initDialog(true);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        timer.cancel();
        timer.purge();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
